package es.ats.codington.festival.daos;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttractions;
import es.ats.codington.festival.domain.TraditionalMarkets;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAOImp;
import es.ats.codington.festival.services.EventDAOService;
import es.ats.codington.festival.utils.FERSDataConnection;

public class EventSignUpDAOImp implements IEventSignUpDAOImp{

	
	public EventSignUpDAOImp (){};

	public int signUpToEvent(int iDvisitor, int iDevent, Connection con) {
		  // TODO Auto-generated method stub
			ResultSet rs;
		  int affectedRows=0;
		try{
			PreparedStatement statement = con.prepareStatement("select * from eventsingup where people_peopleId=? and event_eventid=?");
			statement.setInt(1, iDvisitor);
			statement.setInt(2, iDevent);
			rs = statement.executeQuery();
			//Check that peopleID and eventID exist.
		if(!rs.wasNull()){
		   statement =con.prepareStatement("INSERT INTO eventsingup (people_peopleId, event_eventid) VALUES (?,?);");

		   statement.setInt(1, iDvisitor);
		   statement.setInt(2, iDevent);
		   
		   con.setAutoCommit(false);

		   affectedRows=statement.executeUpdate();
		   
		   con.commit();
		   
		}
		else{
			System.out.println("User register yet in this event.");
		}
		
		  }catch (Exception ee) {
		   System.out.println(" registerDAO 1: "+ ee.getMessage());
		   
	
		   return 0;
		  }
		  
		  if(affectedRows!=0){ //SignUp Correct
		   return 1;
		  }
		  else{	 //SignUp Incorrect
		  return 0;
		  }
		 }


		 public int unsignUpToEvent(int iDvisitor, int iDevent,  Connection con){
		  // TODO Auto-generated method stub
		  int affectedRows=0;
		try{
			PreparedStatement statement;

		   if (con == null) throw new IOException("No se conecta a la base de datos");
		   con.setAutoCommit(false);

		   statement = con.prepareStatement("DELETE FROM eventsingup WHERE people_peopleId=? and event_eventid=?");
		   
		   statement.setInt(1, iDvisitor);
		   statement.setInt(2, iDevent);
		   
		   affectedRows=statement.executeUpdate();
		   con.commit();
		  
		  }catch (Exception ee) {
		   System.out.println(" registerDAO 1: "+ ee.getMessage());
		   return 0;
		  }
		  
		  if(affectedRows!=0){
		   return 1;
		  }
		  else{
		  return 0;
		  }
		  
		 }

		 
		 public ArrayList<Event> eventForUser(int userId, Connection con){
			ArrayList<Event> listIdEvents = new ArrayList<Event>();
			PreparedStatement statement = null;
			ResultSet resultSet = null;
			EventDAOService eventDAO=new EventDAOService();
			
			try{
				statement = con.prepareStatement("select * from eventsingup where people_peopleId = ?");
				statement.setInt(1, userId);
				resultSet = statement.executeQuery();
				while(resultSet.next()){
				listIdEvents.add(eventDAO.getEventByIdService(resultSet.getInt("event_eventid")));	
				}
				resultSet.close();
			}
			catch (SQLException e){
				System.out.println(e.getMessage());
			}
			return listIdEvents;
		 }
		 
	public ArrayList<Event> noEventForUser(int userId, Connection con){
			ArrayList<Event> listIdEvents = new ArrayList<Event>();
			PreparedStatement statement = null;
			ResultSet resultSet = null;
			EventDAOService eventDAO=new EventDAOService();
			PreparedStatement pstm = null;
			ResultSet rs = null;
			int size;
			try{
				statement = con.prepareStatement("SELECT eventid FROM event WHERE eventid NOT IN (SELECT event_eventid FROM eventsingup WHERE people_peopleId= ?)");
				statement.setInt(1, userId);
				resultSet = statement.executeQuery();
				while(resultSet.next()){
					size = 0;
					pstm=con.prepareStatement("select singupid from eventsingup where event_eventid=?");
					pstm.setInt(1,resultSet.getInt("eventid"));
					rs=pstm.executeQuery();
					while(rs.next()){
						size++;
					}
					listIdEvents.add(eventDAO.getEventByIdService(resultSet.getInt("eventid"),Integer.toString(size)));
		
				}
				resultSet.close();
			}
			catch (SQLException e){
				System.out.println(e.getMessage());
			}
			return listIdEvents;
		 }

}
